package reliability.ui;

import javax.swing.*;
import javax.swing.filechooser.FileFilter;
import java.io.File;

public class GsdFileChooser
    extends JFileChooser
{

    public static String ACCEPTABLE_EXTENSION = "gsd";

    private class GsdFileFilter
        extends FileFilter
    {

        public boolean accept(File f)
        {
            if (f.isDirectory())
                return true;

            String extension = GsdFileChooser.getExtension(f);
            return (extension != null) && (extension.equals(ACCEPTABLE_EXTENSION));
        }

        public String getDescription()
        {
            return "Структура графа (.gsd)";
        }
    }

    public GsdFileChooser()
    {
        super();
        setCurrentDirectory(new File("."));
        setAcceptAllFileFilterUsed(false);
        setFileFilter(new GsdFileFilter());
    }

    public static String getExtension(File file)
    {
        String ext = null;
        String fileName = file.getName();
        int i = fileName.lastIndexOf('.');

        if (i > 0 &&  i < fileName.length() - 1)
        {
            ext = fileName.substring(i + 1).toLowerCase();
        }
        return ext;
    }

}
